Flutter 应用实现条形码、二维码的扫描与生成

您所在的位置:网站首页 flutter qrcode Flutter 应用实现条形码、二维码的扫描与生成

Flutter 应用实现条形码、二维码的扫描与生成

#Flutter 应用实现条形码、二维码的扫描与生成| 来源: 网络整理| 查看: 265

一、前言

我们开发原生的时候可以使用zxing进行生成条形码与二维码,但是flutter的插件中只找到了二维码的生成与扫描,业务需求里面需要条形码的生成,需要在原有的基础上进行修改。

二、效果图

image

image

三、扫码逻辑

通过MethodChannel调用原生代码,进行效果的实现

const MethodChannel _channel = const MethodChannel(‘qr_scan’)

四、具体实现方案 1.使用的第三方库

qrscan: ^0.2.17

2.主要代码 class MyApp extends StatefulWidget { MyApp({Key key}) : super(key: key); _MyAppState createState() => _MyAppState(); } class _MyAppState extends State { String barcode = null; Uint8List bytes = Uint8List(0); @override initState() { super.initState(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('二维码生成器'), ), body: Container( margin: EdgeInsets.only(top: 50), child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ Container( width: 200, height: 200, child: Image.memory(bytes), ), // _createBody(), Text(barcode ?? "请扫描相关的条形码"), MaterialButton( onPressed: scan, child: Text("Scan"), color: Colors.blue, textColor: Colors.white, ), MaterialButton( onPressed: () => _generateBarCode(barcode), child: Text("生成普通二维码"), color: Colors.blue, textColor: Colors.white, ), MaterialButton( onPressed: () => _generateBarCode1(barcode), child: Text("生成条形码"), color: Colors.blue, textColor: Colors.white, ), ], ), ) ), ); } ///扫描二维码 Future scan() async { try { String barcode = await scanner.scan(); print("这是扫描出来的结果"+barcode); setState(() { this.barcode = barcode; bytes=Uint8List(0); // ClipboardData data = new ClipboardData(text:barcode); // Clipboard.setData(data); }); } on Exception catch (e) { if (e == scanner.CameraAccessDenied) { setState(() { this.barcode = 'The user did not grant the camera permission!'; }); } else { setState(() => this.barcode = 'Unknown error: $e'); } } on FormatException { setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)'); } catch (e) { setState(() => this.barcode = 'Unknown error: $e'); } } Future _generateBarCode(String inputCode) async { Uint8List result = await scanner.generateBarCode(inputCode); this.setState(() => this.bytes = result); } Future _generateBarCode1(String inputCode) async { Uint8List result = await scanner.generateBarCode1(inputCode); this.setState(() => this.bytes = result); } } 五、注意事项

通过以上操作只能实现扫描与生成二维码的操作,条形码的生成还需要以下步骤进行修改。

1.flutter端添加关键代码

image

Future generateBarCode1(String code) async { assert(code != null && code.isNotEmpty); return await _channel.invokeMethod('generate_barcode1', {"code": code}); }

2.Android端添加关键代码

image

case "generate_barcode1": this.result = result; generateQrCode1(call); break; private void generateQrCode1(MethodCall call) { String code = call.argument("code"); Bitmap bitmap = createImage1(code, 200, 200, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] datas = baos.toByteArray(); this.result.success(datas); } public static Bitmap createImage1(String text, int w, int h, Bitmap logo) { if (TextUtils.isEmpty(text)) { return null; } try { //条形码CODE_128 BarcodeFormat fomt=BarcodeFormat.CODE_128; BitMatrix matrix=new MultiFormatWriter().encode(text, fomt, w, h); int width=matrix.getWidth(); int height=matrix.getHeight(); int[] pixel=new int[width*height]; for(int i=0;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3